<?php

/**
 * 商品搜索
 */

namespace app\shop\common;

class SearchCommon extends \app\base\common\SiteCommon {

    protected $pageLimit = 10;

    protected function index() {

        $name = request('', 'keyword');
        $name = str_len(html_clear(urldecode($name)), 10, false);
        if(empty($name)) {
            $this->error('请输入搜索关键词!');
        }

        $this->pageInfo['name'] = $name . ' - 搜索';
        $this->setMeta($name . ' - 搜索');
        $this->setCrumb([
            [
                'name' =>  $name,
                'url' => url()
            ]
        ]);

        $pageLimit = 15;

        $service = target('shop/List', 'service');


        $service->setUrlParam('keyword', $name);


        $filterWhere = [];
        $filterWhere['_sql'] = 'B.title like "%'.$name.'%"';
        $data = $service->getData($filterWhere, ['keyword' => $name]);

        $filter = $data['filter'];
        $where = $data['where'];
        $filterStatus = $data['filterStatus'];
        $order = $data['order'];

        $priceList = $service->getPriceData($filter['ids']);
        $brandList = $service->getBrandData($filter['ids']);

        $orderList = $service->getOrderData();

        $model = target('shop/Shop');
        if($where) {
            $count = $model->countList($where);
            $pageData = $this->pageData($count, $pageLimit, $filter['urlParam']);
            $list = $model->loadList($where, $pageData['limit'], $order);
        }else {
            $pageData = [];
            $list = [];
        }

        $this->assign('attrList', $filter['attrList']);
        $this->assign('brandList', $brandList);
        $this->assign('filterStatus', $filterStatus);
        $this->assign('priceList', $priceList);
        $this->assign('orderList', $orderList);
        $this->assign('page', $pageData['html']);
        $this->assign('pageList', $list);
        $this->siteDisplay('shop_search');
    }


}